﻿<%@ WebHandler Language="C#" Class="ZSGJCheckSubmitBillHandler" %>

using System;
using System.Web;
using BusinessRule.DTO;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data.OracleClient;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using Newtonsoft.Json;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;

public class ZSGJCheckSubmitBillHandler : IHttpHandler, IRequiresSessionState
{
    CurrentUserInfo m_UserInfo;
    QulityCheckBusiness GJcheck = new QulityCheckBusiness();
    uLMSDealFaultBillIbusiness m_DealFaultBill = uMESBusinessFactory.instance.GetuLMSdealFaultBillObject();
    #region 页面初始化
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            switch (action)
            {
                case "GetGJCheckSubmitBill"://故检单--(分页)查询
                    GetGJCheckSubmitBill(context);
                    break;
                case "SaveGJCheckSubmitBill"://故检单--插入/保存
                    SaveGJCheckSubmitBill(context);
                    break;
                case "SubmitGJCheckSubmitBill"://故检单--提交
                    SubmitGJCheckSubmitBill(context);
                    break;
                case "FinishGJCheckSubmitBill"://故检单--完成
                    FinishGJCheckSubmitBill(context);
                    break;
                ///---------部组件入库------------   
                case "GetMfgOrderParts":
                    GetMfgOrderParts(context);
                    break;
                case "SubmitParts":
                    SubmitParts(context);
                    break;
                //---------首件鉴定/质量评审/工艺评审------------
                case "GetFirstIdentifyProductData":
                    GetFirstIdentifyProductData(context);
                    break;   
                case"SecureFirstIdentifyProduct":
                    SecureFirstIdentifyProduct(context);
                    break;        
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    #endregion

    #region 方法
    /// <summary>
    /// 故检单--(分页)查询
    /// </summary>
    /// <param name="context"></param>
    protected void GetGJCheckSubmitBill(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        string pageSize = context.Request["pageSize"].ToString();
        string pageNumber = context.Request["pageNumber"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("Machine", dt.Rows[0]["Machine"].ToString());
        para.Add("MfgOrdername", dt.Rows[0]["MfgOrdername"].ToString());
        para.Add("Status", dt.Rows[0]["Status"].ToString());
        para.Add("startedate", dt.Rows[0]["startedate"].ToString());
        para.Add("enddate", dt.Rows[0]["enddate"].ToString());
        para.Add("ProductType", dt.Rows[0]["ProductType"].ToString());
        para.Add("PageSize", pageSize);
        para.Add("CurrentPageIndex", pageNumber);
        uMESProcessResult result = GJcheck.GetGJCheckSubmitBill(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 故检单--插入/保存
    /// </summary>
    /// <param name="context"></param>
    protected void SaveGJCheckSubmitBill(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var ZSGJCHECKSUBMITBILLID = dt.Rows[0]["ZSGJCHECKSUBMITBILLID"].ToString();

        var strSequence = "000000" + GJcheck.GetGJCheckSubmitBillCount();
        var noticeBillNo = "GJ-" + DateTime.Now.ToString("yyyy") + "-" + m_UserInfo.FactoryCode + "-" + strSequence.Substring(strSequence.Length - 4);
        var para = new Dictionary<string, string>();
        para.Add("ZSGJCHECKSUBMITBILLID", string.IsNullOrWhiteSpace(ZSGJCHECKSUBMITBILLID) ? Guid.NewGuid().ToString() : ZSGJCHECKSUBMITBILLID);//唯一ID
        para.Add("ZSGJCHECKSUBMITBILLNO", noticeBillNo);
        para.Add("MFGORDERID", dt.Rows[0]["MfgorderID"].ToString());//订单
        para.Add("GJCHECKFINISHDATE", dt.Rows[0]["GJCHECKFINISHDATE"].ToString());//故检完成日期
        para.Add("PRODUCTTYPE", dt.Rows[0]["PRODUCTTYPE"].ToString());//产品类别
        para.Add("ISQUALIFIED", dt.Rows[0]["ISQUALIFIED"].ToString());//是否合格
        para.Add("DISCOVERPROPLEM", dt.Rows[0]["DISCOVERPROPLEM"].ToString());//问题
        para.Add("TAKEMEASURES", dt.Rows[0]["TAKEMEASURES"].ToString());//采取措施
        para.Add("CREATEEMPLOYEEID", m_UserInfo.EmployeeID.ToString());//创建人
        result.Result = string.IsNullOrWhiteSpace(ZSGJCHECKSUBMITBILLID) ? GJcheck.InsertGJCheckSubmitBill(para) : GJcheck.UpdateGJCheckSubmitBill(para);
        result.Message = result.Result ? "保存成功" : "保存失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 故检单--提交
    /// </summary>
    /// <param name="context"></param>
    protected void SubmitGJCheckSubmitBill(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSGJCHECKSUBMITBILLID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSGJCHECKSUBMITBILLID", ZSGJCHECKSUBMITBILLID);
        para.Add("SUBMITEMPLOYEEID", m_UserInfo.EmployeeID.ToString());
        result.Result = GJcheck.UpdateGJCheckSubmitBill(para);
        result.Message = result.Result ? "提交成功" : "提交失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 故检单--完成
    /// </summary>
    /// <param name="context"></param>
    protected void FinishGJCheckSubmitBill(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ZSGJCHECKSUBMITBILLID = context.Request["ids"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ZSGJCHECKSUBMITBILLID", ZSGJCHECKSUBMITBILLID);
        para.Add("Status", "3");
        result.Result = GJcheck.UpdateGJCheckSubmitBill(para);
        result.Message = result.Result ? "操作成功" : "操作失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    #endregion

    #region 查询部件待入库信息
    /// <summary>
    /// 查询部件待入库信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetMfgOrderParts(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        string pageSize = context.Request["pageSize"].ToString();
        string pageNumber = context.Request["pageNumber"].ToString();
        var para = new Dictionary<string, string>();
        var YearMonth = dt.Rows[0]["YearMonth"].ToString().Replace("-","");
        para.Add("YearMonth",YearMonth.ToString());
        para.Add("ContainerNo", dt.Rows[0]["ContainerNo"].ToString());
        para.Add("MfgOrdername", dt.Rows[0]["MfgOrdername"].ToString());
        para.Add("PageSize", pageSize);
        para.Add("CurrentPageIndex", pageNumber);
        result= GJcheck.GetMfgOrderParts(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 组件入库--提交
    /// </summary>
    /// <param name="context"></param>
    protected void SubmitParts(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var para=new Dictionary<string,string>();
         para.Add("CollecterpmaterialID",Guid.NewGuid().ToString());
         para.Add("MfgOrderName",dt.Rows[0]["MfgOrderName"].ToString());
         para.Add("ProductBatchNo", dt.Rows[0]["ProductBatchNo"].ToString());
         para.Add("ProductName",dt.Rows[0]["ProductName"].ToString());
         para.Add("Qty",dt.Rows[0]["Qty"].ToString());
         uMESProcessResult result = new uMESProcessResult();
         result.Result=m_DealFaultBill.InsertCollectErpmaterial(para);
         result.Message = result.Result ? "提交成功" : "提交失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    #endregion
    /// <summary>
    /// 首件鉴定/质量评审/工艺评审
    /// </summary>
    /// <param name="context"></param>
    private void GetFirstIdentifyProductData(HttpContext context) 
    {
        uMESProcessResult result = new uMESProcessResult();
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        string pageSize = context.Request["pageSize"].ToString();
        string pageNumber = context.Request["pageNumber"].ToString();
        var selectSQL = @"SELECT FIRSTIDENTIFYPRODUCTID,decode(IDENTIFYTYPE,'1','首件鉴定','2','质量评审','3','工艺评审','未知') IDENTIFYTYPE,PRODUCTNAME,
                        decode(STATUS,'1','已解除','未解除') STATUS,BUILDUNIT,DRAWINGNO,IDENTIFYTYPE FTYPE,
                        KEYIMPTFILELOGO,QPRDLEVEL,REMARK,VERSIONNO,r.employeename HANDLEEMPLOYEENO FROM FIRSTIDENTIFYPRODUCT f 
                        LEFT JOIN (
	                        SELECT wm_concat(e.FULLNAME) employeename,s.FACTORYID from roledef ro 
				                                            left JOIN employeerole erl ON erl.roleid=ro.roleid
				                                            left JOIN employee e on e.employeeid=erl.employeeid
				                                            LEFT JOIN SESSIONVALUES s ON s.SESSIONVALUESID =e.SESSIONVALUESID 
				                                            where ro.rolename like '%控制点解除处理人员' GROUP BY s.FACTORYID
                        ) r ON r.FACTORYID=f.FACTORYID  where 1=1 ";
        if (!string.IsNullOrWhiteSpace(dt.Rows[0]["DrawingNo"].ToString())) { selectSQL += " and DRAWINGNO like '%" + dt.Rows[0]["DrawingNo"].ToString() + "%'"; }
        if (!string.IsNullOrWhiteSpace(dt.Rows[0]["ProductName"].ToString())) { selectSQL += " and ProductName like '%" + dt.Rows[0]["ProductName"].ToString() + "%'"; }
        if (!string.IsNullOrWhiteSpace(dt.Rows[0]["IdentifyType"].ToString())) { selectSQL += " and IdentifyType = '" + dt.Rows[0]["IdentifyType"].ToString() + "'"; }
        if (!string.IsNullOrWhiteSpace(dt.Rows[0]["Status"].ToString())) { selectSQL += " and Status= '" + dt.Rows[0]["Status"].ToString() + "'"; }
        
        selectSQL += " ORDER BY IDENTIFYTYPE,FIRSTIDENTIFYPRODUCTID DESC ";
        var paraPage = new uMESPagingDataDTO();
        paraPage.CurrentPageIndex = Convert.ToInt32(pageNumber);
        paraPage.PageSize = Convert.ToInt32(pageSize);
        paraPage.strSQL = selectSQL;
        result = DatabaseHelper.RunProcedureForPage(paraPage);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 控制点解除
    /// </summary>
    /// <param name="context"></param>
    private void SecureFirstIdentifyProduct(HttpContext context)
    {
        var id = context.Request["id"].ToString();
        var type = context.Request["type"].ToString();
        var updateSQL = @"update FIRSTIDENTIFYPRODUCT set STATUS=1,REMARK=REMARK||'【" + m_UserInfo.FullName + "】手动处理解除' where FIRSTIDENTIFYPRODUCTID='" + id + "' and IDENTIFYTYPE='"+type+"'";
        uMESProcessResult result = new uMESProcessResult();
        result.Result = DatabaseHelper.ExecuteSql(updateSQL) > 0 ? true : false;
        result.Message = result.Result ? "解除成功" : "解除失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

}